cmake_minimum_required(VERSION 3.16)
project(replay_encoder_standalone)

# Usage example (Windows PowerShell):
#  $env:OHOS_NDK_HOME="F:/DevEcoStudio/sdk/default/openharmony/ndk"  # 修改为你的 NDK 路径
#  cmake -S . -B out/arm64 -D CMAKE_TOOLCHAIN_FILE=$env:OHOS_NDK_HOME/build/cmake/ohos.toolchain.cmake -D OHOS_ARCH=arm64-v8a -D CMAKE_BUILD_TYPE=Release
#  cmake --build out/arm64 --config Release -j
#  产物: out/arm64/libreplay_encoder.so

if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    message(FATAL_ERROR "CMAKE_TOOLCHAIN_FILE (ohos.toolchain.cmake) is required. Set OHOS_NDK_HOME/build/cmake/ohos.toolchain.cmake.")
endif()

if(NOT DEFINED OHOS_ARCH)
    set(OHOS_ARCH arm64-v8a)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# 源码根目录（当前文件所在目录的上级）
set(REPLAY_ENCODER_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

add_library(replay_encoder SHARED
    ${REPLAY_ENCODER_SRC_DIR}/NapiBridge.cpp
    ${REPLAY_ENCODER_SRC_DIR}/ReplayEncoder.cpp
)
set_target_properties(replay_encoder PROPERTIES OUTPUT_NAME "replay_encoder")

target_include_directories(replay_encoder PRIVATE
    ${REPLAY_ENCODER_SRC_DIR}
)

# 允许通过 -D 显式指定库路径（推荐优先传入绝对路径，避免查找失败）
set(ACE_NAPI_LIB "" CACHE FILEPATH "Path to libace_napi(.z).so")
set(AVCODEC_LIB  "" CACHE FILEPATH "Path to libavcodec(.z).so")
set(AVMUXER_LIB  "" CACHE FILEPATH "Path to libavmuxer(.z).so")

# 推断 native 根目录与候选库目录
get_filename_component(_TC_DIR ${CMAKE_TOOLCHAIN_FILE} DIRECTORY)
get_filename_component(_NATIVE_DIR ${_TC_DIR} DIRECTORY) # .../native/build/cmake -> .../native/build
get_filename_component(_NATIVE_DIR ${_NATIVE_DIR} DIRECTORY) # .../native

set(_CAND_LIB_DIRS
    ${CMAKE_SYSROOT}/usr/lib/${OHOS_ARCH}
    ${CMAKE_SYSROOT}/lib/${OHOS_ARCH}
    ${_NATIVE_DIR}/lib/${OHOS_ARCH}
    ${_NATIVE_DIR}/libs/${OHOS_ARCH}
    ${_NATIVE_DIR}/lib
    ${_NATIVE_DIR}/libs
)

if(NOT ACE_NAPI_LIB)
    find_library(ACE_NAPI_LIB NAMES ace_napi ace_napi.z HINTS ${_CAND_LIB_DIRS} REQUIRED)
endif()
if(NOT AVCODEC_LIB)
    find_library(AVCODEC_LIB  NAMES avcodec avcodec.z HINTS ${_CAND_LIB_DIRS} REQUIRED)
endif()
if(NOT AVMUXER_LIB)
    find_library(AVMUXER_LIB  NAMES avmuxer avmuxer.z HINTS ${_CAND_LIB_DIRS} REQUIRED)
endif()

target_link_libraries(replay_encoder PRIVATE
    ${ACE_NAPI_LIB}
    ${AVCODEC_LIB}
    ${AVMUXER_LIB}
)

message(STATUS "OHOS_ARCH=${OHOS_ARCH}")
message(STATUS "ACE_NAPI_LIB=${ACE_NAPI_LIB}")
message(STATUS "AVCODEC_LIB=${AVCODEC_LIB}")
message(STATUS "AVMUXER_LIB=${AVMUXER_LIB}")


